import os
import sys
from dotenv import load_dotenv
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.models.knowledge_base import KnowledgeBase
from loguru import logger
# 加载环境变量
load_dotenv()

# 配置路径
INPUT_DIR = os.getenv('INPUT_DIR', './input')
KNOWLEDGE_BASE_PATH = os.getenv('KNOWLEDGE_BASE_PATH', './knowledge')

def main():
    """构建知识库的主函数"""
    logger.info(f"*****构建知识库==> 输入文件夹 {INPUT_DIR} 知识库文件夹 {KNOWLEDGE_BASE_PATH}*****")
    try:
        print(f"开始构建知识库...")
        print(f"输入目录: {INPUT_DIR}")
        print(f"知识库存储路径: {KNOWLEDGE_BASE_PATH}")
        
        # 初始化知识库
        kb = KnowledgeBase(INPUT_DIR, KNOWLEDGE_BASE_PATH)
        
        # 构建向量存储
        kb.build_vector_store()
        
        print("知识库构建完成!")
        return 0
    except Exception as e:
        print(f"构建知识库失败: {e}")
        return 1

if __name__ == '__main__':
    sys.exit(main())    